Ext.ns("CRM.SPComp");

(function(){


	
			CRM.SPComp.createLeadDashboardSearchComp = function(gridid,dataStore, columnModel,assignedToLeadStore,gradeDashboardStore,stageDashboardStore){
	       	
		    //for image
				
			var editor = new Ext.ux.grid.RowEditor();	
	     	var gridView = new Ext.grid.GridView();
	     	var selModel = new Ext.grid.RowSelectionModel({singleSelect : true});
	     	var spSearchLeadDashboardGridPanel = new Ext.grid.GridPanel({
	       	      autoHeight 	: true,
	       	      id			: gridid,
	       	      width			: 1070,
	       	      iconCls		: 'lead_user_image',
	       	      title			: 'Lead Details',
	       	      view 			: gridView,
	       	      store			: dataStore,
	       	      colModel 		: columnModel,		
	       	      selModel 		: selModel,
	       	      stripeRows	: true,
	       	      enableRowBody : true,
	       	      scrollable	: true,
	       	      frame			: true
	       	   });		
		
	     	var leadDashboardGridPanel = new Ext.Panel({
	     		width    : 1100,
	     		items 	: [
							{
								xtype		: 'panel',
								id			: 'combinePanel',
								autoHeight	: true,
								width		: 1100,
								frame		: true,
								padding		: '5px',
								items		: [
												 {
												 xtype       : 'fieldset',
											     id          : 'criteriaPanel',
											     title 		: 'Search Criteria',
												 height      : 65,
											     width       : 1070,
											     labelWidth  : 120,
											     layout      : 'hbox',
											     items       : [
											                      {
											                    	 xtype : 'compositefield',
											                    	 items : [
											                       				{ xtype : 'displayfield', value : 'Assigned To : '},
											                       				{
																				    xtype        : 'combo',
																				    id           : 'assignedToLead',
																				    valueField   : 'id',
																				    value        :'SELECT',
																				    store        : assignedToLeadStore,
																				    displayField : 'name',
																				    triggerAction: 'all',
																				    value        : 'SELECT',
																				    typeAhead    : false,
																				    editable     : false,
																				    //width        : 330,
																				    listeners	: {select : function(){
																				        						bindValue('assignedToLead', 'assignedToLeadId');
																				        					} 
																				        			  }
																				},
																				{
												                       			    	xtype		: 'hidden',
												                       			    	id			: 'assignedToLeadId'
												                       			 },
												                       			{ xtype : 'displayfield', value : 'Grade: '},
											                       				{
											                    	   				xtype 		: 'combo',
											                    	   				id	  		: 'gradeDashboard',
											                    	   				store		:  gradeDashboardStore,
											                                        displayField: 'name',
											                                        valueField  : 'id',
											                                        value		: 'SELECT',
											                                        triggerAction:'all',
											                    	   				width 		: 180,
											                    	   				editable	: false,
											                    	   			    listeners	: {select : function(){
											                    						bindValue('gradeDashboard', 'gradeDashboardId');
											                    						
											                    					  } 
											                    			      }
											                       				},
											                       				{
											                       			    	xtype		: 'hidden',
											                       			    	id			: 'gradeDashboardId'
											                       			    },
											                       			    { xtype : 'displayfield', value : 'Stage: '},
											                       				{
											                    	   				xtype 		: 'combo',
											                    	   				id	  		: 'stageDashboard',
											                    	   				store		:  stageDashboardStore,
											                                        displayField: 'name',
											                                        valueField  : 'id',
											                                        value		: 'SELECT',
											                                        triggerAction:'all',
											                    	   				width 		: 180,
											                    	   				editable	: false,
											                    	   			    listeners	: {select : function(){
											                    						bindValue('stageDashboard', 'stageDashboardId');
											                    						
											                    					  } 
											                    			        }
											                       				 },
											                       				 {
											                       			    	xtype		: 'hidden',
											                       			    	id			: 'stageDashboardId'
											                       			     },
											                       				 {
												                       				    xtype 	: 'button',
													                       				text  	: 'Search',
													                       				handler : function(){
													                       							doSearch(false,spSearchLeadDashboardGridPanel);
													                       						 }
												                       				    },
											                       				    {
											                       				    	xtype	: 'button',
											                       				    	text	: 'Clear Search',
											                       				    	handler : function(){
											                       				    				//var dashboardGrid = Ext.getCmp('spSearchLeadDashboardGridPanel');
												                       				    		spSearchLeadDashboardGridPanel.store.removeAll();
											                       				    			 }
											                       				    }
											                       				
											                    			]
											                       }]
												 		},
														spSearchLeadDashboardGridPanel
													]
							}
						]
	     	
	     	});
	    	return leadDashboardGridPanel;
			};
			
	     	function doSearch(ONKEYEVENT,leadDashBoardgrid){
	     		var assignedToLeadId = Ext.getCmp('assignedToLeadId').getValue();
	     		var gradeDashboardId = Ext.getCmp('gradeDashboardId').getValue();
	     		var stageDashboardId = Ext.getCmp('stageDashboardId').getValue();
	     		
	     		if(assignedToLeadId == 'SELECT' && gradeDashboardId == 'SELECT' && stageDashboardId == 'SELECT')
				{
 					Ext.Msg.alert('Alert','Please Select a Criteria');
 				}
 				
 				if(assignedToLeadId != 'SELECT' && gradeDashboardId != 'SELECT' && stageDashboardId != 'SELECT')
 				{
// 					var dashboardGrid = Ext.getCmp('spSearchLeadDashboardGridPanel');
 					leadDashBoardgrid.store.removeAll();
 					leadDashBoardgrid.store.reload({
			    			params: {
 										assignedToId	: assignedToLeadId,
 										gradeId 		: gradeDashboardId,
 										stageId			: stageDashboardId
			    					}
			    				});
 				}
	     	};
		
	
	
	

})();